<?php
include_once "BaseM.php";
 
class UserM extends BaseM{
	
	public $table = "user";
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function insert($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $result;
	}

	public function update($obj) {	  
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;	
	}
 
	public function delete($key) {
		$sql="delete from $this->table where id = $key";
		$count = @mysql_query( $sql ) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $count;
	}
	
	public function getTableData($key) {
		$sql = "
			select 
				id, account, password, name, date_create
			from $this->table 
			where id = $key";
		return $this->querySQL($sql, true);
	}
	
	public function queryTable($orderData, $startPage, $pageRow, $searchStr) {
		$whereStr = " 1 = 1 $searchStr ";
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql = "
			select 
				id, account, name, date_create
			from $this->table
			where $whereStr
			order by $orderData 
			limit $startPage, $pageRow ";
		$result = $this->querySQL($resultSql);
		
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function plusLoginFail($account) {
		$sql = "update user set fail_times = fail_times + 1 where account = '$account' ";
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;	
	}

	public function getUser($account, $password) {
		$sql = "select * from $this->table where ( account='$account' ) and ( ps_e='$password' )";
		return $this->querySQL($sql, true);
	}
	
	public function updateLiveKey($obj){
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		return @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
	}
	
	public function insertLogUserLoginout($obj){
		$sql = $this->genInserSQL("log_user_loginout", $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $result;
	}
	
}

?>